<template>
    <el-dialog :title="title" v-model="drawer" width="700px" :append-to-body="true" @close="close">
        <el-form :model="form" :rules="rules" size="small" label-width="150px" ref="ruleRefForm" class="refForm">
            <el-form-item label="名称:" prop="name">
                <el-input v-model="form.name" placeholder="请输入名称"></el-input>
            </el-form-item>
            <el-form-item label="副标题:" prop="subName">
                <Editor v-model="form.subName" :type="1"/>
            </el-form-item>
            <el-form-item label="分类:" >
                <el-select v-model="form.categoryId" class="m-2" placeholder="请选择分类">
                    <el-option v-for="item in categorys" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="题型:" prop="type">
                <el-select v-model="form.type" class="m-2" placeholder="请选择题型">
                    <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="form.descs" type="textarea" />
            </el-form-item>

            <template v-if="form.type == '1' || form.type == '4' || form.type == '5'">
                <el-divider />
                <el-button type="warning" plain @click="addChoices()">+ 添加选项</el-button>
                <el-table :data="form.qnChoices" style="width: 100%">
                    <el-table-column prop="orderNo" label="排序" width="120">
                        <template #default="scope">
                            <el-input-number v-model="scope.row.orderNo" :min="0"></el-input-number>
                        </template>
                    </el-table-column>
                    <el-table-column prop="indexLetter" v-if="form.type != '4'" label="选项索引" width="120">
                        <template #default="scope">
                            <el-input v-model="scope.row.indexLetter" placeholder="例如：A,B,C ..."></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="content" label="选项内容">
                        <template #default="scope">
                            <el-input v-model="scope.row.content" type="textarea" placeholder="请输入"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" fixed="right">
                        <template #default="scope">
                            <el-button type="primary" link size="small" @click="removeRow(scope.$index)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </template>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button type="primary" size="small" @click="submitForm(ruleRefForm)">保存</el-button>
                <el-button size="small" @click="resetForm(ruleRefForm)">取消</el-button>
            </span>
        </template>
    </el-dialog>



</template>

<script>
import {
    computed,
    reactive,
    toRefs,
    ref
} from 'vue'
import {
    ElMessage
} from 'element-plus'
import { create, update } from '/@/api/qn/question'
import Editor from '/@/components/editor/index.vue'
export default {
    name: 'QuestionDetail',
    props: {
        dialogDrawer: {
            type: Boolean,
            default: false
        },
        defaultForm: {
            type: Object,
            default() {
                return {}
            }
        },
        title: {
            type: String,
            default: '新增'
        },
        categoryOptions:{
            type: Array,
            default: []
        }
    },
    components: {
        Editor
    },
    emits: ['onDetailSubmit', 'update:dialogDrawer'],
    setup(props, context) {
        const {
            dialogDrawer,
            defaultForm,
            categoryOptions
        } = toRefs(props)
        const ruleRefForm = ref()
        const drawer = computed({
            get: () => dialogDrawer.value,
            set: val => {
                context.emit('update:dialogDrawer', val)
            }
        })
        const categorys = computed({
            get: () => categoryOptions.value,
            set: val => {
                context.emit('update:dialogDrawer', val)
            }
        })
        let form = computed({
            get: () => defaultForm.value,
            set: val => {
                context.emit('update:defaultForm', val)
            }
        })
        const state = reactive({
            valueFormat: 'YYYY-MM-DD HH:mm:ss',
            rules: {
                name: [{
                    required: true,
                    message: '请输入名称',
                    trigger: 'blur'
                }],
                type: [{
                    required: true,
                    message: '请选择题型',
                    trigger: 'blur'
                }],
            },
            dialogShopVisible: false,
            typeOptions: [{
                value: '1',
                label: '单选',
            },
            {
                value: '2',
                label: '填空',
            },
            {
                value: '3',
                label: '简答题',
            }, {
                value: '4',
                label: '判断',
            }, {
                value: '5',
                label: '多选',
            }]
        })
        const close = () => {
            context.emit('update:dialogDrawer', false)
        }
        const submitForm = async (formEl) => {
            if (!formEl) return
            await formEl.validate((valid) => {
                if (valid) {
                    let data = form.value
                    if (data.id != null && data.id != '') {
                        update(data).then(res => {
                            if (res.code == 0) {
                                ElMessage.success("修改成功");
                                context.emit('onDetailSubmit', true)
                            } else {
                                ElMessage.error("修改失败");
                            }
                        })
                    }
                    // 新增
                    else {
                        create(data).then(res => {
                            if (res.code == 0) {
                                ElMessage.success("添加成功");
                                context.emit('onDetailSubmit', true)
                            } else {
                                ElMessage.error("添加失败");
                            }
                        })

                    }
                }
            })

        }
        const resetForm = (formEl) => {
            if (!formEl) return
            context.emit('update:dialogDrawer', false)
            formEl.resetFields()
        }
        //移除商品
        const removeRow = (index) => {
            form.value.qnChoices.splice(index, 1)
        }

        const addChoices = () => {
            if(form.value.type == '4' && form.value.qnChoices.length == 2){
                ElMessage.error("判断题只能添加两条选项");
                return ;
            }

            form.value.qnChoices.push({
                orderNo: 1,
                indexLetter: 'A',
                content: null
            })
        }


        return {
            drawer,
            form,
            submitForm,
            resetForm,
            close,
            ruleRefForm,
            removeRow,
            addChoices,
            categorys,
            ...toRefs(state)
        }
    }

}
</script>

<style>

</style>
